package com.android.hidephotosplus.util;

import group.pals.android.lib.ui.lockpattern.LockPatternActivity;
import android.app.Activity;
import android.content.Intent;

import com.actionbarsherlock.app.SherlockFragment;
import com.android.hidephotosplus.db.DbHelper;
import com.android.hidephotosplus.preference.BooleanPref;

public class PasswordManager {

	public static PasswordManager passwordManager = null;

	private Activity activity;

	private DbHelper mDbHelper;

	public PasswordManager(Activity activity) {
		this.activity = activity;
		mDbHelper = DbHelper.getInstance(activity);
	}

	public static PasswordManager getInstances(Activity activity) {
		if (passwordManager == null) {
			synchronized (PasswordManager.class) {
				passwordManager = new PasswordManager(activity);
			}
		}
		return passwordManager;
	}

	public void checkSetupOrUnlockPass() {
		String valuePattern = mDbHelper.getValue(DbHelper.KEY_PATTERN_PASSWORD);
		String valueClassic = mDbHelper.getValue(DbHelper.KEY_CLASSIC_PASSWORD);
		if (valuePattern == null && valueClassic == null) {
			setUpPassWord(Config.PATTERN_PASSWORD);
		} else if (valuePattern != null && valueClassic == null) {
			startUnlockPattern();
		} else if (valuePattern == null && valueClassic != null) {

		}

	}

	public void checkSetupOrUnlockPassFragment(SherlockFragment fragment) {
		String valuePattern = mDbHelper.getValue(DbHelper.KEY_PATTERN_PASSWORD);
		String valueClassic = mDbHelper.getValue(DbHelper.KEY_CLASSIC_PASSWORD);
		if (valuePattern == null && valueClassic == null) {
			setUpPassWordFragment(fragment, Config.PATTERN_PASSWORD);
		} else if (valuePattern != null && valueClassic == null) {
			startUnlockPatternFragment(fragment);
		} else if (valuePattern == null && valueClassic != null) {

		}

	}

	public void setUpPassWordFragment(SherlockFragment fragment,
			int type_password) {
		if (type_password == Config.PATTERN_PASSWORD) {
			Intent intent = new Intent(
					LockPatternActivity.ACTION_CREATE_PATTERN, null, activity,
					LockPatternActivity.class);
			fragment.getSherlockActivity().startActivityFromFragment(fragment,
					intent, Config.REQUEST_CODE_SETUP_PATTERN_PASSWORD);
		} else {

		}
	}

	public void startUnlockPatternFragment(SherlockFragment fragment) {
		Intent intent = new Intent(LockPatternActivity.ACTION_COMPARE_PATTERN,
				null, activity, LockPatternActivity.class);
		String valuePattern = mDbHelper.getValue(DbHelper.KEY_PATTERN_PASSWORD);
		char[] value = valuePattern.toCharArray();
		intent.putExtra(LockPatternActivity.EXTRA_PATTERN, value);
		intent.putExtra(LockPatternActivity.EXTRA_PATTERN_INVISIBLE,
				BooleanPref.getPreference(activity, Config.KEY_PREF_INVISIBLE,
						false));
		fragment.getSherlockActivity().startActivityFromFragment(fragment,
				intent, Config.REQUEST_CODE_START_PATTERN_PASSWORD);
	}

	public void setUpPassWord(int type_password) {
		if (type_password == Config.PATTERN_PASSWORD) {
			Intent intent = new Intent(
					LockPatternActivity.ACTION_CREATE_PATTERN, null, activity,
					LockPatternActivity.class);
			activity.startActivityForResult(intent,
					Config.REQUEST_CODE_SETUP_PATTERN_PASSWORD);
		} else {

		}
	}

	public void startUnlockPattern() {
		Intent intent = new Intent(LockPatternActivity.ACTION_COMPARE_PATTERN,
				null, activity, LockPatternActivity.class);
		String valuePattern = mDbHelper.getValue(DbHelper.KEY_PATTERN_PASSWORD);
		char[] value = valuePattern.toCharArray();
		intent.putExtra(LockPatternActivity.EXTRA_PATTERN, value);
		intent.putExtra(LockPatternActivity.EXTRA_PATTERN_INVISIBLE,
				BooleanPref.getPreference(activity, Config.KEY_PREF_INVISIBLE,
						false));
		activity.startActivityForResult(intent,
				Config.REQUEST_CODE_START_PATTERN_PASSWORD);
	}

}
